package com.scau.clothes.api.stock;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.scau.clothes.core.core.entity.AjaxResult;
import com.scau.clothes.stock.model.entity.Size;
import com.scau.clothes.stock.service.SizeService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/size")
@RestController
public class SizeController {
    @Resource
    private SizeService sizeService;

    @PostMapping("/listAllSize")
    @ApiOperation("查询尺寸")
    public AjaxResult listAllSize(@RequestBody Size size){
        List<Size> list = sizeService.listAllSize(size);
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("/insertSize")
    @ApiOperation("新增尺寸")
    public AjaxResult insertSize(@RequestBody Size size){
        int rows = sizeService.insertSize(size);
        return rows > 0 ? AjaxResult.success("添加尺寸成功") : AjaxResult.error("添加尺寸失败");
    }

    @PostMapping("/updateSize")
    @ApiOperation("修改尺寸信息")
    public AjaxResult updateSize(@RequestBody Size size){
        int rows = sizeService.updateSize(size);
        return rows > 0 ? AjaxResult.success("修改尺寸信息成功") : AjaxResult.error("修改尺寸信息失败");
    }

    @PostMapping("/deleteSize")
    @ApiOperation("删除尺寸")
    public AjaxResult deleteSize(@RequestBody Size size){
        int rows = sizeService.deleteSize(size);
        return rows > 0 ? AjaxResult.success("删除尺寸成功") : AjaxResult.error("删除尺寸失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页操作")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Size size){
        List<Size> list = sizeService.listAllSize(size);
        Page page = new Page(pageNum, pageSize);
        int total = list.size();
        page.setTotal(total);
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize,total);
        page.addAll(list.subList(startIndex,endIndex));
        PageInfo pageInfo = new PageInfo<>(page);
        return AjaxResult.success(pageInfo);
    }
}
